package com.shujia.flink.table

import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.table.api.EnvironmentSettings
import org.apache.flink.table.api.bridge.scala.StreamTableEnvironment

object Demo3FLinkSqlOnKafka {


  def main(args: Array[String]): Unit = {

    //创建flink 环境
    val bsEnv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    bsEnv.setParallelism(1)

    //设置table 环境的一些参数
    val bsSettings: EnvironmentSettings = EnvironmentSettings.newInstance()
      .useBlinkPlanner() //使用blikc计划器
      .inStreamingMode() //流模式
      .build()

    // 创建flink  table 环境
    val bsTableEnv: StreamTableEnvironment = StreamTableEnvironment.create(bsEnv, bsSettings)

    /*

mary,2022-03-25 10:30:05,/home
mary,2022-03-25 10:30:06,/admin
mary,2022-03-25 10:30:07,/login
mary,2022-03-25 10:30:08,/home
mary,2022-03-25 11:00:05,/admin
mary,2022-03-25 11:30:05,/admin
mary,2022-03-25 12:00:05,/admin
mary,2022-03-25 12:10:05,/admin
mary,2022-03-25 12:20:05,/admin
mary,2022-03-25 13:00:05,/admin

     */

    /**
      * 读取kafka中的数据 -- 创建source表
      *
      */

    bsTableEnv.executeSql(
      """
        |CREATE TABLE clicks (
        | `user` STRING,
        | cTime TIMESTAMP(3),
        | url STRING,
        | WATERMARK FOR cTime AS cTime - INTERVAL '5' SECOND -- 设置水位线
        |) WITH (
        | 'connector' = 'kafka',
        | 'topic' = 'clicks1',
        | 'properties.bootstrap.servers' = 'master:9092,node1:9092,node2:9092',
        | 'properties.group.id' = 'asdasd',
        | 'format' = 'csv',
        | 'csv.field-delimiter' = ',' ,
        | 'csv.ignore-parse-errors' = 'true',
        | 'scan.startup.mode' = 'earliest-offset'
        |)
        |
      """.stripMargin)

    /**
      * 创建print表
      * LIKE: 应用原表的字段创建新的表
      */

    bsTableEnv.executeSql(
      """
        |CREATE TABLE print_table WITH ('connector' = 'print')
        |LIKE clicks (EXCLUDING ALL)
        |
      """.stripMargin)


    /**
      * 打印结果
      *
      */
    /* bsTableEnv.executeSql(
       """
         |insert into print_table
         |select * from clicks
         |
       """.stripMargin)*/

    /**
      * 统计用户访问网页的次数
      *
      */


    bsTableEnv.executeSql(
      """
        |CREATE TABLE count_print (
        | `user` STRING,
        | c BIGINT
        |)
        |WITH ('connector' = 'print')
        |
      """.stripMargin)

   /* bsTableEnv.executeSql(
      """
        |insert into count_print
        |select `user`,count(url) as c from clicks group by `user`
        |
      """.stripMargin)*/

    /**
      * 每隔一小时统计用户访问网页的次数
      *
      */


    bsTableEnv.executeSql(
      """
        |CREATE TABLE window_print (
        | `user` STRING,
        | win_end TIMESTAMP(3),
        | c BIGINT
        |)
        |WITH ('connector' = 'print')
        |
      """.stripMargin)

    /**
      * 将结果保存到mysql
      * 需要现在mysql中创建表
      *
      */
    bsTableEnv.executeSql(
      """
        |CREATE TABLE mysql_sink (
        | `user` STRING,
        |  win_end TIMESTAMP(3),
        |  c BIGINT,
        |  PRIMARY KEY (`user`,win_end) NOT ENFORCED
        |) WITH (
        |   'connector' = 'jdbc',
        |   'url' = 'jdbc:mysql://master:3306/bigdata',
        |   'table-name' = 'window_count',
        |   'username' = 'root',
        |   'password' = '123456'
        |)
        |
      """.stripMargin)


    bsTableEnv.executeSql(
      """
        |insert into mysql_sink
        |select
        |`user`,
        |TUMBLE_END(cTime, INTERVAL '1' HOURS) as win_end,
        |count(url) as c
        |from
        |clicks group by
        |`user`,
        |TUMBLE(cTime, INTERVAL '1' HOURS) -- 一个小时一个窗口，事件时间的滚动窗口
        |
      """.stripMargin)


  }


}
